package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ICollegesDAO;
import org.jsola.hr.dto.CollegesAddDTO;
import org.jsola.hr.dto.CollegesUpdateDTO;
import org.jsola.hr.entity.CollegesDO;
import org.jsola.hr.query.CollegesQuery;
import org.jsola.hr.service.ICollegesService;
import org.jsola.hr.vo.CollegesListVO;
import org.jsola.hr.vo.CollegesVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 卫人云-院校
 *
 * @author zhr
 */
@Service("hrCollegesServiceImpl")
public class CollegesServiceImpl implements ICollegesService {

    @Autowired
    private ICollegesDAO collegesDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public CollegesVO save(CollegesAddDTO collegesAddDTO, TokenUser tokenUser) {
        // 转DO
        CollegesDO collegesDO = collegesAddDTO.to(CollegesDO.class);
        // 保存
        collegesDO = save(collegesDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return collegesDO.to(CollegesVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(CollegesUpdateDTO collegesUpdateDTO, TokenUser tokenUser) {
        //转DO
        CollegesDO collegesDO = collegesUpdateDTO.to(CollegesDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(collegesDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... collegesIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) collegesIds);
    }


    @Override
    public CollegesVO selectById(String collegesId, String siteId) {
        CollegesDO collegesDO = selectDOById(collegesId, siteId);
        if (collegesDO == null) {
            return null;
        }
        return collegesDO.to(CollegesVO.class);
    }

    @Override
    public List<CollegesListVO> select(CollegesQuery collegesQuery, String siteId) {
        List<CollegesDO> collegesDOList = selectDO(collegesQuery, siteId);
        if(CollectionUtils.isEmpty(collegesDOList)) {
            return collegesDOList == null ? null : new ArrayList<>();
        }
        return collegesDOList.stream()
                .map(collegesDO -> collegesDO.to(CollegesListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(CollegesQuery collegesQuery, String siteId) {
        Example example = buildExample(collegesQuery,siteId);
        return collegesDAO.selectCountByExample(example);
    }

    @Override
    public Page<CollegesListVO> selectPage(CollegesQuery collegesQuery, String siteId) {
        Example example = buildExample(collegesQuery,siteId);
        Page<CollegesDO> page = collegesDAO.selectPageByExample(example,
                collegesQuery.getPageNo(),
                collegesQuery.getPageSize());

        return page.to(CollegesListVO.class);
    }

    @Override
    public CollegesDO selectDOById(String collegesId, String siteId) {
        return listById(collegesId, siteId);
    }

    @Override
    public List<CollegesDO> selectDO(CollegesQuery collegesQuery, String siteId) {
        Example example = buildExample(collegesQuery,siteId);
        return collegesDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param collegesQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(CollegesQuery collegesQuery, String siteId) {
        Example example = new Example(CollegesDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,collegesQuery.getOrders());
        return example;
    }
}




